package com.hzncc.scaffold.controller.Front.Assessment;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Assessment;
import com.hzncc.scaffold.entity.Vo.ContentAndTitleVo;
import com.hzncc.scaffold.service.AssessmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-15 12:23
 * @Description: null
 */
@RequestMapping("/assessment")
@ApiOperation("前台接口-质量评价")
@RestController
public class AssessmentController {

    @Autowired
    AssessmentService assessmentService;


    @PostMapping("/commit")
    @ApiOperation("提交质量评价")
    public ResultT commit(@RequestBody ContentAndTitleVo contentAndTitleVo){

        try {
            StpUtil.checkLogin();
        }catch (NotLoginException notLoginException){
            return ResultT.fail("用户未登录");
        }

        if(!StpUtil.isLogin()){
            return ResultT.fail("用户未登录");
        }
        String content = contentAndTitleVo.getContent();
        String title = contentAndTitleVo.getTitle();

        Assessment assessment = new Assessment();
        assessment.setTitle(title);
        assessment.setContent(content);
        assessment.setUserId(StpUtil.getLoginIdAsInt());
        assessment.setCreateTime(DateUtil.now());

        boolean save = assessmentService.save(assessment);
        if(save){
            return ResultT.success("提交成功");
        }else {
            return ResultT.fail("提交失败");
        }
    }
}
